﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SQLServerDialog
{
    public partial class SQLServerFolderFileDialog : Form
    {
        private SQLServerFolderFileBrowser sqlBrowser;
        private TreeNode parent;
        private delegate void initMethodDelegate();
        private initMethodDelegate initMethod;

        public string Server { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
        public string SelectedLocation { get; set; }
        public bool SkipDotPrefixFolders { get; set; }
        public bool SkipDotPrefixFiles { get; set; }
        public bool IsShowFiles { get; set; }
        public string Filter { get; set; }
        public Image ImageDrive { get { return treeviewIcon.Images[0]; } set { treeviewIcon.Images[0] = value; } }
        public Image ImageClosedFolder { get { return treeviewIcon.Images[1]; } set { treeviewIcon.Images[1] = value; } }
        public Image ImageOpenedFolder { get { return treeviewIcon.Images[2]; } set { treeviewIcon.Images[2] = value; } }
        public Image ImageFile { get { return treeviewIcon.Images[3]; } set { treeviewIcon.Images[3] = value; } }

        public SQLServerFolderFileDialog()
        {
            InitializeComponent();
            SkipDotPrefixFolders = true;
            SkipDotPrefixFiles = true;
            Filter = "";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Dispose();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {           
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.SelectedLocation = treeView.SelectedNode.Tag.ToString();
            this.Dispose();
        }

        private void SQLServerFolderFileDialog_Load(object sender, EventArgs e)
        {
            sqlBrowser = new SQLServerFolderFileBrowser(this.Server,this.Username,this.Password);
            initMethod = initDrives;
            RenderTree();
        }

        private void treeView_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            if (e.Node.GetNodeCount(true) > 0)
            {
                parent = e.Node;
                initMethod = initDir;
                RenderTree();
            }            
        }

        private void RenderTree()
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                initMethod();
                Cursor.Current = Cursors.Default;
            }
            catch(Exception ex) {
                MessageBox.Show(ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
                this.Dispose();
            }
        }

        private void initDrives()
        {
            List<string> drives = sqlBrowser.GetDrives();            
            foreach (var drive in drives)
            {
                parent = treeView.Nodes.Add(drive);
                parent.Tag = string.Format("{0}:", drive);
                parent.ImageIndex = 0;
                //if sub folder exist, show collapse(+)
                if (sqlBrowser.GetSubFolders(parent.Tag.ToString(),this.SkipDotPrefixFolders).Count > 0)
                    parent.Nodes.Add(":)");
            }
        }

        private void initDir()
        {
            parent.Nodes.Clear();
            List<string> folders = sqlBrowser.GetSubFolders(parent.Tag.ToString(), this.SkipDotPrefixFolders);
            TreeNode newNode;
            foreach (var folder in folders)
            {
                newNode = parent.Nodes.Add(folder);
                newNode.Tag = string.Format("{0}\\{1}", parent.Tag, folder);
                newNode.ImageIndex = 1;
                newNode.SelectedImageIndex = 2;
                //if sub folder exist, show collapse(+)
                if (sqlBrowser.GetSubFolders(newNode.Tag.ToString(), this.SkipDotPrefixFolders).Count > 0 || sqlBrowser.GetFiles(newNode.Tag.ToString(), this.SkipDotPrefixFiles, this.Filter).Count > 0)
                    newNode.Nodes.Add(":)");
            }

            if (this.IsShowFiles)
            {
                List<string> files = sqlBrowser.GetFiles(parent.Tag.ToString(), this.SkipDotPrefixFiles,this.Filter);
                foreach (string file in files)
                {
                    newNode = parent.Nodes.Add(file);
                    newNode.Tag = string.Format("{0}\\{1}", parent.Tag, file);
                    newNode.ImageIndex = 3;
                    newNode.SelectedImageIndex = 3;
                }
            }
        }
    }
}
